package com.example.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.crm.entity.Contact;
import com.example.crm.mapper.ContactMapper;
import com.example.crm.service.ContactService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 联系人服务实现类
 */
@Service
public class ContactServiceImpl extends ServiceImpl<ContactMapper, Contact> implements ContactService {

    @Override
    public IPage<Contact> listContactsByPage(Map<String, Object> params) {
        int pageNum = Integer.parseInt(params.getOrDefault("page", "1").toString());
        int pageSize = Integer.parseInt(params.getOrDefault("limit", "10").toString());
        
        QueryWrapper<Contact> queryWrapper = new QueryWrapper<>();
        
        // 根据客户ID筛选
        if (params.containsKey("customerId") && params.get("customerId") != null) {
            queryWrapper.eq("customer_id", params.get("customerId"));
        }
        
        // 根据姓名筛选
        if (params.containsKey("name") && StringUtils.isNotBlank(params.get("name").toString())) {
            queryWrapper.like("name", params.get("name"));
        }
        
        // 根据手机号筛选
        if (params.containsKey("mobile") && StringUtils.isNotBlank(params.get("mobile").toString())) {
            queryWrapper.like("mobile", params.get("mobile"));
        }
        
        // 根据是否为首要联系人筛选
        if (params.containsKey("isPrimary") && params.get("isPrimary") != null) {
            queryWrapper.eq("is_primary", params.get("isPrimary"));
        }
        
        // 排序
        queryWrapper.orderByDesc("create_time");
        
        return page(new Page<>(pageNum, pageSize), queryWrapper);
    }

    @Override
    public Contact getContactDetail(Long id) {
        return getById(id);
    }

    @Override
    public List<Contact> getContactsByCustomerId(Long customerId) {
        QueryWrapper<Contact> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id", customerId);
        queryWrapper.orderByDesc("is_primary");
        queryWrapper.orderByDesc("create_time");
        return list(queryWrapper);
    }

    @Override
    public Contact getPrimaryContactByCustomerId(Long customerId) {
        QueryWrapper<Contact> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id", customerId);
        queryWrapper.eq("is_primary", true);
        return getOne(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean setPrimaryContact(Long contactId, Long customerId) {
        // 先将所有联系人设为非首要
        QueryWrapper<Contact> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id", customerId);
        
        Contact updateContact = new Contact();
        updateContact.setIsPrimary(false);
        update(updateContact, queryWrapper);
        
        // 设置新的首要联系人
        Contact contact = getById(contactId);
        if (contact != null && contact.getCustomerId().equals(customerId)) {
            contact.setIsPrimary(true);
            return updateById(contact);
        }
        return false;
    }

    @Override
    public boolean addContact(Contact contact) {
        // 如果是首要联系人，则需要将该客户的其他联系人设为非首要
        if (contact.getIsPrimary() != null && contact.getIsPrimary()) {
            setPrimaryContact(null, contact.getCustomerId());
        }
        return save(contact);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateContact(Contact contact) {
        // 如果是首要联系人，则需要将该客户的其他联系人设为非首要
        if (contact.getIsPrimary() != null && contact.getIsPrimary()) {
            QueryWrapper<Contact> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("customer_id", contact.getCustomerId());
            queryWrapper.ne("id", contact.getId());
            
            Contact updateContact = new Contact();
            updateContact.setIsPrimary(false);
            update(updateContact, queryWrapper);
        }
        return updateById(contact);
    }
} 